from .dantic import ParamSetting
from typing import List

from common import (
    TaskStatus,
    Result,
)

import logging

from ExtendRegister.model_register import Setting

LOGGER = logging.getLogger(__name__)


class HomeService:

    def getModelName(self):
        setting:Setting = Setting.query.filter(Setting.settingKey == "modelName").first()
        if setting is None:
            return None
        return setting.settingValue
    


    def getSetting(self):
        res = {}
        settings: List[Setting] = Setting.query.all()
        for s in settings:
            res[s.settingKey] = s.settingValue
        return Result().setData(res)

    def saveSetting(self, body: ParamSetting):
        setting = body.dict()
        for key, value in setting.items():
            s: Setting = Setting.query.filter(Setting.settingKey == key).first()
            if s is None:
                s = Setting()
                s.settingKey = key
                s.settingValue = value
            else:
                s.settingValue = value

            s.save()

        return Result()
